package com.chain.BrotherExchange.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.DataSource;
import com.chain.BrotherExchange.ui.adapter.TextWatcherAdapter;
import com.example.common.widget.state.NetworkStateView;

/**
 * 输入交易密码
 */
public class TransactionPasswordDialog extends Dialog implements NetworkStateView {

    private final View mProgressBar;
    private final TextView mErrorMessageText;
    private final EditText mTransactionPsdEdit;

    public static void show(Context context, DataSource.DataCallback<TransactionPasswordDialog> callback) {
        new TransactionPasswordDialog(context, callback).show();
    }

    private TransactionPasswordDialog(Context context, DataSource.DataCallback<TransactionPasswordDialog> callback) {
        super(context);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_transaction_password, null);
        addContentView(contentView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

        // 进度
        mProgressBar = contentView.findViewById(R.id.progress_bar);

        // 错误信息
        mErrorMessageText = contentView.findViewById(R.id.error_message);

        // 交易密码
        mTransactionPsdEdit = contentView.findViewById(R.id.transaction_password_edit);
        observePassword();

        // 提交数据
        View affirmButton = contentView.findViewById(R.id.affirm_button);
        affirmButton.setOnClickListener(v -> {
            String password = mTransactionPsdEdit.getText().toString();
            if (TextUtils.isEmpty(password)) {
                mErrorMessageText.setText(R.string.hint_input_transaction_password);
            } else {
                callback.onResult(this);
            }
        });

        // 关闭对话框
        ImageView closeImage = contentView.findViewById(R.id.close_image);
        closeImage.setOnClickListener(v -> dismiss());
    }

    private void observePassword() {
        mTransactionPsdEdit.addTextChangedListener(new TextWatcherAdapter(s -> {
            if (s.length() > 0) {
                mErrorMessageText.setText(null);
            }
        }));
    }

    public String getPassword() {
        return mTransactionPsdEdit.getText().toString();
    }

    @Override
    public void show() {
        super.show();
        getWindow().setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().getDecorView().setBackgroundColor(Color.parseColor("#f7f7f7"));
        getWindow().setAttributes(params);
    }

    @Override
    public void loadingNetwork() {
        mProgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void successNetwork() {
        mProgressBar.setVisibility(View.GONE);
        dismiss();
    }

    @Override
    public void errorNetwork(CharSequence charSequence) {
        mProgressBar.setVisibility(View.GONE);
        mErrorMessageText.setText(charSequence);
    }

    @Override
    public void notNetwork() {

    }

    @Override
    public void emptyNetwork() {

    }

    @Override
    public void onAnewRequestNetwork() {

    }
}
